﻿using System;
using System.Linq;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using WebApi.Common;
using WebApi.Common.HelpOp;
using WebApi.Common.MiddleWare;
using WXQ.BusinessCore.CommonManager;
using WXQ.Enties;
using WXQ.Enties.CommonObj;
using WXQ.InOutPutEntites.Input;
using WXQ.InOutPutEntites.Input.configmanager.Dict;
using WXQ.InOutPutEntites.Output;

namespace WebApi.Controllers.configmanager
{
    /// <summary>
    /// 字典管理
    /// </summary>
    [Route("Api/Configmanage/Dict")]
    [Authorize]
    public class DictController : BaseController
    {
        /// <summary>
        /// 添加字典
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Log]
        [Route("InsertDict")]
        public JsonResult InsertDict([FromBody] InsertDictInput model)
        {
            var result = new ResponseResult();

            var userId = UserOp.GetUserId(User);
            var op = new DictOp(userId);

            var r = new Dict
            {
                Description = model.Description,
                DictKey = model.DictKey,
                DictValue = model.DictValue,
                DictType = model.DictType,
                OrderBy = model.OrderBy,
                GroupName = model.GroupName,
                AddDateTime = DateTime.Now,
                AddUser = User.Identity?.Name
            };

            var rv = op.InsertDict(r);

            if (!rv)
            {
                result.Code = ResponseResultMessageDefine.OpLost;
                result.Errors.Add(ResponseResultMessageDefine.OpLostMessage);
            }

            return Json(result);
        }

        /// <summary>
        /// 修改字典
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Log]
        [Route("UpdateDict")]
        public JsonResult UpdateDict([FromBody] UpdateDictInput model)
        {
            var result = new ResponseResult();

                var userId = UserOp.GetUserId(User);
                var op = new DictOp(userId);

                var r = new Dict
                {
                    DictId = model.DictId,
                    Description = model.Description,
                    DictKey = model.DictKey,
                    DictValue = model.DictValue,
                    DictType = model.DictType,
                    OrderBy = model.OrderBy,
                    GroupName = model.GroupName,
                    UpdateDateTime = DateTime.Now,
                    UpdateUser = User.Identity?.Name
                };

                var rv = op.UpdateDict(r);

                if (!rv)
                {
                    result.Code = ResponseResultMessageDefine.OpLost;
                    result.Errors.Add(ResponseResultMessageDefine.OpLostMessage);
                }

            return Json(result);
        }

        /// <summary>
        /// 删除字典通过主键
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet]
        [Log]
        [Route("DeleteDictByDictId")]
        public JsonResult DeleteDictByDictId([FromQuery] int Id)
        {
            var result = new ResponseResult();

            if (Id <= 0)
            {
                result.Code = ResponseResultMessageDefine.ParaError;
                result.Errors.Add("字典id错误");
            }
            else
            {
                var userId = UserOp.GetUserId(User);
                var op = new DictOp(userId);

                var rv = op.DeleteDictByDictId(Id);

                if (!rv)
                {
                    result.Code = ResponseResultMessageDefine.OpLost;
                    result.Errors.Add(ResponseResultMessageDefine.OpLostMessage);
                }
            }

            return Json(result);
        }

        /// <summary>
        /// 删除字典根据分组名
        /// </summary>
        /// <param name="GroupName"></param>
        /// <returns></returns>
        [HttpPost]
        [Log]
        [Route("DeleteDictByGroupName")]
        public JsonResult DeleteDictByGroupName([FromQuery] string GroupName)
        {
            var result = new ResponseResult();

            if (!string.IsNullOrEmpty(GroupName))
            {
                result.Code = ResponseResultMessageDefine.ParaError;
                result.Errors.Add("分组名不可为空");
            }
            else
            {
                var userId = UserOp.GetUserId(User);
                var op = new DictOp(userId);

                var rv = op.DeleteDictByGroupName(GroupName);

                if (!rv)
                {
                    result.Code = ResponseResultMessageDefine.OpLost;
                    result.Errors.Add(ResponseResultMessageDefine.OpLostMessage);
                }
            }

            return Json(result);
        }

        /// <summary>
        /// 搜索列表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Log]
        [Route("GetDictList")]
        public JsonResult GetDictList([FromBody] GetDictListInput model)
        {
            var result = new ResponseResult();

                var op = new DictOp();

                var pagemodel = model.Page.Adapt<PageInput, PageModel>();
                var rv = op.GetDictList(model.GroupName, pagemodel);

                if (rv == null)
                {
                    result.Code = ResponseResultMessageDefine.OpLost;
                    result.Errors.Add(ResponseResultMessageDefine.OpLostMessage);
                }
                else
                {
                    result.Data = rv;
                }

            return Json(result);
        }

        /// <summary>
        /// 搜索列表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Log]
        [Route("GetGroupList")]
        public JsonResult GetGroupList([FromBody] GetDictListInput model)
        {
            var result = new ResponseResult();

                var op = new DictOp();

                var pagemodel = model.Page.Adapt<PageInput, PageModel>();
                var rv = op.GetDictList("dictgroup", pagemodel);

                if (rv == null)
                {
                    result.Code = ResponseResultMessageDefine.OpLost;
                    result.Errors.Add(ResponseResultMessageDefine.OpLostMessage);
                }
                else
                {
                    result.Data = rv;
                }

            return Json(result);
        }
    }
}